package beans;

import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

/**
 * Session Bean implementation class Primaa
 */
@Stateless
@LocalBean
public class Primaa implements PrimaaRemote {

	@Resource(mappedName = "jms/msgmessages")
	private ConnectionFactory connectionFactory;
	
	@Resource(mappedName = "jms/msgtopic")
	private Topic topic;
	
	MessageProducer messageProducer;
	TextMessage textMessage;
	Session session;
	Connection connection;
	
    /**
     * Default constructor. 
     */
    public Primaa() {
        // TODO Auto-generated constructor stub
    }
    
   
	public void salut() {
    	try{
    		connection = connectionFactory.createConnection();
    		
    		session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
    		
    		messageProducer = session.createProducer(topic);
    		textMessage = session.createTextMessage();
    		textMessage.setText("mesajul 1");
    		messageProducer.send(textMessage);
    		textMessage.setText("mesajul 2");
    		messageProducer.send(textMessage);
    		textMessage.setText("KTHXBYE!");
    		messageProducer.send(textMessage);
    		
    		messageProducer.close();
    		session.close();
    		connection.close();
   	
    	}
    	catch (JMSException e) {
    		e.printStackTrace();
    	}
    }
	

}
